// pages/myindex/myindex.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isCheck: 0,
    id: null,
    mine: false,
    list: [],
    pageNumber: 1,
    pageSize: 10,
    totalPages: 1,
    user: {
      avatarUrl: '',
      realName: '',
    },
    runningNum: 0,
    cancelNum: 0,
    isPrivate: '',
    userName:''
  },

  tapCheck() {
    this.setData({
      isPrivate: this.data.isPrivate == 0 ? 1 : 0
    })
    this.saveInfo(this.data.isPrivate,this.data.id)
  },


  saveInfo(isPrivate,id) {
    var that = this;
    console.log(isPrivate);
    console.log(id);
    app.getUserInfo(function (userInfo) {
      app.req({
        url: "wechat/user/updateUserInformation",
        data: {
          id:id,
          isPrivate: isPrivate
        },
        method: "post",
        success: function (res) {
          if (200 == res.code) {
            console.log(res);
            console.log(userInfo);
            app.personalData(function (e) {
            })
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    });
  },



  
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },

  formatTime(dateTime) {
    var date = new Date((dateTime.replace(/\-/g, "/")));
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    return year + '/' + month + '/' + day;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var user;
    app.getUserInfo(function(userInfo) {
      user = userInfo;
      that.toggle("userInfo", userInfo);
      that.toggle("id", options.id || userInfo.id);
      that.toggle("mine", ((options.id && (options.id == userInfo.id)) || (userInfo.id && !options.id)));
      that.getListData(false);
      that.setData({
        isPrivate:user.isPrivate,
        userName: userInfo.realName
      })
    });
    console.log(that.data);
  },
  toDetail: function(event) {
    if (this.data.mine) {
      var dataset = event.currentTarget.dataset;
      wx.navigateTo({
        url: '/pages/register/register?id=' + dataset.id
      })
    }
  },
  getListData: function(append) {
    var that = this;
    var data = that.data;
    app.req({
      url: app.globalData.url + "wechat/activity/selectActivity?uid=" + data.id,
      method: "post",
      header: {
        'api-client': '8693ece21a2711ea995200163e05ff66',
        'api-version': '1.3.1',
      },
      data: {
        type: 0,
        pageSize: data.pageSize,
        pageNumber: data.pageNumber
      },
      success: function(res) {
        if (200 == res.code) {
          var arr = res.data.page.rows;
          for (let i = 0; i < arr.length; i++) {
            res.data.page.rows[i].actStartTimeDate = that.formatTime(arr[i].actStartTime);

            var avatarUrl = data.user.avatarUrl;
            var realName = data.user.realName;
            var userInfo = {
              avatarUrl: arr[i].createUrl,
              realName: arr[i].createName,
            };
            that.toggle("user", userInfo);
          }
          var list = data.list;
          if (append) {
            that.toggle("list", list.concat(res.data.page.rows));
          } else {
            that.toggle("list", res.data.page.rows);
          }
          that.toggle("totalPages", res.data.page.totalPages);
          // that.calculateRunningCancelNum();
          that.toggle("runningNum", res.data.processCount);
          that.toggle("cancelNum", res.data.overCount);
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    });
  },
  calculateRunningCancelNum () {
    var tmpRunning = 0;
    var tmpCancel = 0;
    for (let i = 0; i < this.data.list.length; i++) {
      if (this.data.list[i].status == 1) {
        tmpRunning ++;
      }
      if (this.data.list[i].status == 2) {
        tmpCancel ++;
      }
    }
    this.toggle("runningNum", tmpRunning);
    this.toggle("cancelNum", tmpCancel);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.toggle("pageNumber", 1);
    this.toggle("totalPages", 1);
    this.getListData(false);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.toggle("pageNumber", 1);
    this.toggle("totalPages", 1);
    this.getListData(false)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    var data = that.data;
    if (data.totalPages > data.pageNumber) {
      that.toggle("pageNumber", data.pageNumber + 1);
      this.getListData(true);
    }
  }
})